#include "login_ciphertext.hpp"
#include <shadow.h>
#include "tlv_type.h"
#include <iostream>

using std::cout;
using std::endl;


namespace rocket 
{

login_ciphertext::login_ciphertext(shared_ptr<rocket::tcp_link>link,rocket::packet data)
:_link(link)
,_data(data)
{

}

login_ciphertext::~login_ciphertext()
{
    
}

void login_ciphertext::something()
{
    //是登录发送密文报文
	cout << "type=LOGIN_CIPHERTEXT" << endl;
    //根据前一次存储的用户名来获取到对应的密文
    spwd *ciphertext = getspnam(_link->get_user().c_str());
	//判断密文是否匹配,并向客户端发送密文匹配结果
	if (strcmp(_data.get_data().c_str(), ciphertext->sp_pwdp) == 0) 
    {
	    cout << "password is correct" << endl;
	    rocket::tlv result_tlv(rocket::LOGIN_RESULT,
				   strlen("success"), "success");
        _link->add_send_task(result_tlv);
	    cout << "send_result_tlv : " ;
	    result_tlv.print();
	} else 
    {
	    cout << "password is wrong" << endl;
	    rocket::tlv result_tlv(rocket::LOGIN_RESULT,
				   strlen("success"), "success");
        _link->add_send_task(result_tlv);
	    cout << "send_result_tlv : " ;
	    result_tlv.print();
	}
}


}